<template>
    <el-scrollbar height="100%" style="overflow-y: hidden;" v-if="flag">
        <el-row type="flex" justify="center" style="align-content: center;height: 100%;margin-top: 2%">
            <el-form ref="ruleFormRef" style="width: 80%;" :model="ruleForm" :rules="rules" label-width="auto"
                :size="formSize" status-icon :label-position="labelPosition">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="上传商品图片">
                            <el-upload :auto-upload="false" list-type="picture-card" ref="uploadRef"
                                :action="`/api/image/upload?label=1&id=${gid}`" :on-preview="handlePictureCardPreview"
                                :on-remove="handleRemove" :before-upload="beforeAvatarUpload" :limit="1"
                                :on-success="handleAvatarSuccess">
                                <el-icon>
                                    <Plus />
                                </el-icon>
                            </el-upload>

                            <el-dialog v-model="dialogVisible">
                                <img w-full :src="dialogImageUrl" alt="Preview Image" />
                            </el-dialog>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="商品名称">
                            <el-input minlength="10" maxlength="100" v-model="ruleForm.commodityName"
                                style="width: 100%" :rows="10" type="text" />
                        </el-form-item>
                        <el-form-item label="售价">
                            <el-input v-model="ruleForm.price" style="width: 100%" :rows="10" type="number" />
                        </el-form-item>
                        <el-form-item label="商品类型">
                            <el-select v-model="ruleForm.type" placeholder="商品类型" size="large" style="width: 240px">
                                <el-option v-for="item in kinds" :key="item.kid" :label="item.type"
                                    :value="item.type" />
                            </el-select>
                        </el-form-item>
                        <el-row justify="space-between">
                            <el-col :span="10">
                                <el-form-item label="商品库存">
                                    <el-input v-model="ruleForm.count" style="width: 100%" :rows="10" type="number" />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-form-item label="商品简介">
                            <el-input v-model="ruleForm.description" style="width: 100%" :rows="5" type="textarea"
                                minlength="15" />
                        </el-form-item>
                        <el-form-item>
                            <el-row style="width: 100%; align-content: center;margin-top: 5%;" justify="center">
                                <el-col :span="14">
                                    <el-button
                                        style="background-color: #fbaf5a; color: white; width: 100%;height: 40px;"
                                        @click="submitForm">提交商品申请</el-button>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </el-row>
    </el-scrollbar>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { UploadFile, UploadProps } from 'element-plus'
import type { FormProps } from 'element-plus'
import { userInfoStore } from '../assets/userStore/userStore'
import { addCommodity, queryAllKind } from '../assets/net/NetUtils';
import type { UploadInstance } from 'element-plus'
import { KindModel } from '../assets/model/Kind'
import { CommodityModel } from '../assets/model/Commodity'
import router from '../router'

const userStore = userInfoStore();
const dialogImageUrl = ref<string>();
const dialogVisible = ref<boolean>(false);
const uploadRef = ref<UploadInstance>();
const kinds = ref<Array<KindModel>>();
const gid = ref<number>();
const flag = ref<boolean>(true)
const labelPosition = ref<FormProps['labelPosition']>('top');
const formSize = ref<ComponentSize>('large');
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<CommodityModel>({
    commodityName: '',
    description: '',
    count: 0,
    uid: userStore.uid,
    type: '',
    price: 0
});


onMounted(() => {
    queryAllKind()
        .then((value) => {
            kinds.value = value.data as Array<KindModel>;
        })
});

const handleRemove: UploadProps['onRemove'] = (file: UploadFile) => {
    console.log(file)
    router.replace('/mycommodities')
};

const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
    dialogImageUrl.value = uploadFile.url!
    dialogVisible.value = true
};

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('图片大小不得超过2MB！')
        return false
    }
    return true
}



const rules = reactive<FormRules<CommodityModel>>({
    commodityName: [
        {
            required: true,
            message: 'Please input Activity name',
            trigger: 'blur'
        }
    ],
    description: [
        {
            required: true,
            message: 'Please select at least one activity type',
            trigger: 'change',
        },
    ]
})

const submitForm = () => {
    addCommodity(ruleForm)
        .then((value) => {
            if (value.code === 200) {
                let commodity: CommodityModel = value.data;
                gid.value = commodity.gid;
                uploadRef.value!.submit();
                ElMessage.success('商品上传成功');
            }
        })
}

const handleAvatarSuccess: UploadProps['onSuccess'] = () => {
    router.replace("/mycommodities")
}
</script>


<style scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}


.header_register {
    width: 100%;
    height: 90px;
    background-color: #fbaf5a;
    padding: 20px;
}
</style>